//
//  FriendViewController.m
//  huanxin
//
//  Created by 郭亚 on 16/4/28.
//  Copyright © 2016年 郭亚. All rights reserved.
//

#import "FriendViewController.h"

#import "SendFriendViewController.h"

#import "ContentViewController.h"

#import <EaseMob.h>

@interface FriendViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation FriendViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self createView];
    self.navigationItem.title = @"好友";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:(UIBarButtonSystemItemAdd) target:self action:@selector(addButtonClicked)];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self reloadFriend];
}

- (void)createView
{
    _dataArray = [NSMutableArray array];
    _tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:(UITableViewStylePlain)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    _tableView.tableFooterView = [[UIView alloc] init];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    // EMBuddy 好友信息描述
    EMBuddy *buddy = _dataArray[indexPath.row];
    // username 好友名字
    cell.textLabel.text = buddy.username;
    
    return cell;
}

- (void)reloadFriend
{

    [[EaseMob sharedInstance].chatManager asyncFetchBuddyListWithCompletion:^(NSArray *buddyList, EMError *error) {
        [_dataArray removeAllObjects];
        [_dataArray addObjectsFromArray:buddyList];
        [_tableView reloadData];
    } onQueue:dispatch_get_main_queue()];
    
}

- (void)addButtonClicked
{
    SendFriendViewController *sendVC = [[SendFriendViewController alloc] init];
    [self.navigationController pushViewController:sendVC animated:YES];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ContentViewController *contentVC = [[ContentViewController alloc] init];
    
    EMBuddy *buddy = _dataArray[indexPath.row];
    contentVC.chatter = buddy.username;
    
    [self.navigationController pushViewController:contentVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
